package com.zlw.main.recorderlib.recorder.mp3;



import com.zlw.main.recorderlib.recorder.RecordConfig;
import com.zlw.main.recorderlib.utils.FileUtils;
import com.zlw.main.recorderlib.utils.Logger;
import ohos.media.common.Format;
import ohos.media.common.Source;
import ohos.media.extractor.Extractor;

import java.io.IOException;

/**
 * @author zhaolewei on 2018/8/3.
 */
public class Mp3Utils {
    private static final String TAG = Mp3Utils.class.getSimpleName();

    /**
     * 获取mp3音频的总时长 单位：ms
     *
     * @param mp3FilePath MP3文件路径
     * @return 时长
     */
    public static long getDuration(String mp3FilePath) {
        if (!FileUtils.isFileExists(mp3FilePath)) {
            return 0;
        }
        if (!mp3FilePath.endsWith(RecordConfig.RecordFormat.MP3.getExtension())) {
            return 0;
        }
        Extractor mex = null;
        try {
            mex = new Extractor();
            mex.setSource(new Source(mp3FilePath));
            Format mf = mex.getStreamFormat(0);
            long duration = mf.getLongValue(Format.DURATION) / 1000L;
            return duration;
        } catch (Exception e) {
            Logger.e(e, TAG, e.getMessage());
        } finally {
            if (mex != null) {
                mex.release();
            }
        }
        return 0;
    }
}
